﻿using System;
using System.ServiceModel;
using System.Threading;
using System.Threading.Tasks;

namespace RandomSearch.Host {
    public class Program {
        
        private static void Main(string[] args) {
            var unloadAll = new ManualResetEvent(false);
            AppDomain.CurrentDomain.DomainUnload += (_, __) => unloadAll.Set();
            
            var t = new Task(() => RunService(unloadAll, args[0]));
            t.Start();
            unloadAll.WaitOne();
        }


        public static void RunService(ManualResetEvent eEvent, string url) {
            using (var host = new ServiceHost(typeof (RandomSearchServer))) {
                var binding = new NetNamedPipeBinding("netPipeConfig");
                host.AddServiceEndpoint(typeof (ISearchServer), binding, url);
                host.Open();
                eEvent.WaitOne();
                host.Close();
            }
        }
    }
}